//
//  ViewController.m
//  what
//
//  Created by 飞奔的羊 on 16/4/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "ViewController.h"
#import "iOS28thStudensDBTool.h"
#import "iOS28thStudents.h"

@interface ViewController ()<UITableViewDataSource,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
 *  <#Description#>
 */
@property (nonatomic,strong)NSArray *students;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.frame = CGRectMake(0, 0, 150, 30);
    searchBar.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = searchBar;
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.students = [iOS28thStudensDBTool queryWithKeyWord:searchText];
    [self.tableView reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.students.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StudentCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"StudentCell"];
    }
    iOS28thStudents *student = self.students[indexPath.row];
    cell.textLabel.text = student.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",student.age];
    return cell;
    
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{


}


- (void)queryAll{
    self.students = [iOS28thStudensDBTool queryAll];
    
    [self.tableView reloadData];
}

- (void)delete{
    //1.创建一个CZStudent
    iOS28thStudents *student = [[iOS28thStudents alloc] init];
    student.name = @"过拜访";
    
    [iOS28thStudensDBTool deleteStudent:student];
}

- (void)update{
    //1.创建一个CZStudent
    iOS28thStudents *student = [[iOS28thStudents alloc] init];
    student.name = @"涛涛";
    student.age = 24;
    
    [iOS28thStudensDBTool updateStudent:student];
}

- (void)insert{
    NSLog(@"%s",__func__);
    
    //1.创建一个CZStudent
    iOS28thStudents *student = [[iOS28thStudents alloc] init];
    student.name = @"川川";
    student.age = 28;
    
    [iOS28thStudensDBTool insertStudent:student];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
